const app=getApp();
Page({
  data: {
      yzmCode:'',
    phone: '',
    smsCode: '',
    password: '',
    password1: '',
    smsCodeButtonText: '获取验证码',
    smsCodeButtonDisabled: false,
    timer: null,
    countdown: 60,
    num:'',
    tsxx:''
  },

  onPhoneInput(event) {
    this.setData({
      phone: event.detail
    });
  },
  onLoad(options){
    this.setData({
        phone:options.phone,
        num:options.num
    })
    console.log(this.data.phone,this.data.num)
  },

  onSmsCodeInput(event) {
    this.setData({
      smsCode: event.detail
    });
  },

  onPasswordInput(event) {
    this.setData({
      password: event.detail
    });
  },

  onPasswordInput1(event) {
    this.setData({
      password1: event.detail
    });
  },

  onGetSmsCode() {
    const { phone } = this.data;

    if (!phone) {
      wx.showToast({
        title: '请输入手机号',
        icon: 'none'
      });
      return;
    }else{
        let url=app.globalData.URL+'/users/yzmCode';
        let data={
            phone:phone
        }
        app.wxRequest('GET',url,data,(res)=>{
              this.setData({
                  yzmCode:res.data
              })
        })
    }

    this.setData({
      smsCodeButtonDisabled: true,
      smsCodeButtonText: `${this.data.countdown}秒`
    });

    this.startCountdown();

    // 在此处调用后台 API 获取验证码
    wx.request({
      url: 'https://your-backend-api.com/getSmsCode', // 替换为你的 API 地址
      method: 'POST',
      data: {
        phone: phone
      },
      success: (res) => {
        wx.showToast({
          title: '验证码已发送',
          icon: 'success'
        });
      },
      fail: (err) => {
        wx.showToast({
          title: '发送失败，请重试',
          icon: 'none'
        });
        this.setData({
          smsCodeButtonDisabled: false,
          smsCodeButtonText: '获取验证码'
        });
        clearInterval(this.data.timer);
      }
    });
  },

  startCountdown() {
    const timer = setInterval(() => {
      let countdown = this.data.countdown - 1;
      if (countdown === 0) {
        clearInterval(this.data.timer);
        this.setData({
          smsCodeButtonDisabled: false,
          smsCodeButtonText: '获取验证码',
          countdown: 60
        });
      } else {
        this.setData({
          countdown: countdown,
          smsCodeButtonText: `${countdown}秒`
        });
      }
    }, 1000);

    this.setData({ timer });
  },

  onRegister() {
    const { phone, smsCode, password, password1 } = this.data;
    
    

    if (!password || !password1 ) {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'none'
      });
      return;
    }

    if (password !== password1) {
      wx.showToast({
        title: '两次输入的密码不一致',
        icon: 'none'
      });
      return;
    }
    
    let url='';
    if(this.data.num==1){
        url=app.globalData.URL+'/users/insert',
        this.setData({
            tsxx:'注册成功'
        })
    }else{
        url=app.globalData.URL+'/users/update',
        this.setData({
            tsxx:'修改成功'
        })
    }
    let data={
        phone:this.data.phone,
        password:this.data.password
    };
    console.log(url,data)
    app.wxRequest('GET',url,data,(res)=>{
        
        if(res.statusCode==200){
            wx.showToast({
                title: this.data.tsxx,
                icon: 'success'
              });
              wx.redirectTo({
                url: '/pages/login/login',
              })
             
        }else{
            console.log('我草拟')
            console.log(res)
        }
       
        
    },(err)=>{
        console.log('我草泥马')
    })

    // 在此处执行注册逻辑，例如调用后端 API
   
    
    wx.redirectTo({
      url: '',
    })
  }
});
